layui.use(['layer', 'form', 'admin', 'laydate', 'ax','layarea','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var layarea = layui.layarea;
    var upload = layui.upload;
    var examUser;
    $('#btn').click(function () {
        new $ax(Feng.ctxPath + "/api/exam/getExamUserInfoByCode?studentCode="+$("#studentCode").val(), function (data){
            if(data.data){
                $("#userId").val(data.data.user_id);
                $("#full_name").val(data.data.full_name);
                $("#name").val(data.data.name);
                $("#school_name").val(data.data.school_name);
                $("#student_code").val(data.data.student_code);
                if(data.data.exam_user){
                    examUser = data.data.exam_user;
                    var str = '<option value="">请选择</option>';
                    data.data.exam_user.forEach(function (d){
                        str=str+"<option value="+d.exam_user_id+" >"+d.examTitle+"</option>";
                    });
                    $("#examUserId").html(str);
                    form.render();
                }
            }
        }).start();
        form.render();
    });

    form.on("select(examUserId)",function (data) {
        var examUserId = data.value;
        examUser.forEach(e=>{
            if( e.exam_user_id == data.value){
                $("#examId").val(e.exam_id);
                $("#admission_card_no").val(e.admission_card_no);
                var examId = e.exam_id;
                new $ax(Feng.ctxPath + "/api/exam/getExamContentByExamId?examId="+examId, function (data){
                    if(data.data){
                        var str = '<option value="">请选择</option>';
                        data.data.forEach(function (d){
                            str=str+"<option value="+d.examContentId+" >"+d.examText +"/"+d.examTime+"</option>";
                        });
                        $("#examContentId").html(str);
                        form.render();
                    }
                }).start();
            }
        })
    });

//多图片上传
    var imgUrls1 = [];
    upload.render({
        elem: '#test2',
        url: Feng.ctxPath + '/api/upload/uploadFile',
        multiple: true,
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="width: 20%">')
            });
        },
        done: function(res){
            //上传完毕
            //如果上传失败
            if (res.code != 200) {
                return layer.msg('上传失败');
            }
            imgUrls1.push(res.data.fileName);
            $("#examWork").val(imgUrls1.toLocaleString());
            //上传成功
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/exam/examResulSave", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });

});